/** EasyWeb iframe v3.1.3 date:2019-07-12 License By http://easyweb.vip */

// 以下代码是配置layui扩展模块的目录，每个页面都需要引入
layui.config({
    version: true,
    base: getProjectUrl() + 'assets/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: 'step-lay/step',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    Cropper: 'Cropper/Cropper',
    zTree: 'zTree/zTree',
    introJs: 'introJs/introJs',
    fileChoose: 'fileChoose/fileChoose',
    tagsInput: 'tagsInput/tagsInput'
}).use(['layer', 'admin'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var admin = layui.admin;

    // 移除loading动画
    setTimeout(function () {
        admin.removeLoading();
    }, window == top ? 600 : 100);

});

// 获取当前项目的根路径，通过获取layui.js全路径截取assets之前的地址
function getProjectUrl() {
    var layuiDir = layui.cache.dir;
    if (!layuiDir) {
        var js = document.scripts, last = js.length - 1, src;
        for (var i = last; i > 0; i--) {
            if (js[i].readyState === 'interactive') {
                src = js[i].src;
                break;
            }
        }
        var jsPath = src || js[last].src;
        layuiDir = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
    }
    return layuiDir.substring(0, layuiDir.indexOf('assets'));
}


// 自定义ajax通用请求
function ajaxCommon(url, data) {
    var $ = layui.jquery;
    var layer = layui.layer;
    $.ajaxSettings.async = false;
    $.post(url, data, function (res) {
        if (res.code == 200) {
            layer.msg(res.msg, {icon: 1, time: 3000});
        } else {
            layer.msg(res.msg, {icon: 2, time: 3000});
        }
    }, 'json');
}

//数值保留两位小数
function fixed(str) {
    if (!str) str = '0.00';
    let ret = Math.round(parseFloat(str) * 100) / 100;
    let decimal = ret.toString().split('.');
    if (decimal.length === 1) {
        return ret.toString() + '.00'
    }
    ;
    if (decimal.length > 1) {
        if (decimal[1].length < 2) {
            return ret.toString() + '0'
        }
        return ret
    }
    ;
    return ret;
}

//此正则表达式验证小数位是否超过两位，小数可为1位 || 2位 || 正整数
function checkNumber(num) {
    if((!(/^[-\\+]?\d+(\.\d{1,2})?$/.test(num) || /^\d$/.test(num))) || num<0){
        return false;
    }else{
        return true;
    }
}

//此正则表达式验证是否是数字，整数 || 正数 || 负数
function checkNumber2(num) {
    if((!(/^[-\\+]?\d+(\.\d{1,2})?$/.test(num) || /^\d$/.test(num)))){
        return false;
    }else{
        return true;
    }
}

//此正则表达式验证是否是数字，正整数
function checkNumber3(num) {
    if (!(/(^[1-9]\d*$)/.test(num))) {
        return false;
    }else{
        return true;
    }
}

var DISABLED = 'layui-btn-disabled';

//单击之后提交按钮不可选,防止重复提交
function btnDisabled(data) {
    // 增加样式
    data.addClass(DISABLED);
    // 增加属性
    data.attr('disabled', 'disabled');
}

// 移除不可点击
function removeBtnDisabled(data) {
    data.removeClass(DISABLED);
    data.removeAttr('disabled');
}

init();

function init(){
    if(document.getElementById('demo_project_title')!=null){
        document.getElementById('demo_project_title').innerHTML="豆豆藤";
    }
    if(document.getElementById('demo_project_title_header')!=null){
        document.getElementById('demo_project_title_header').innerHTML="豆豆藤";
    }
}

function hideButton(jquery){
    var $ = jquery;
    var buttons = [];
    $(".role_menu").each(function () {
        var button = $(this).data("button");
        if (buttons.indexOf(button) < 0) {
            buttons.push(button);
        }

    });
    if (buttons.length > 0) {
        $.post("/sys/role/buttonRole/", {
            buttons: JSON.stringify(buttons)
        }, function (data) {
            if (data.code == 200) {
                var menuList = data.data;
                var hideButtons = getArrDifference(buttons, menuList);

                for (var i = 0; i < hideButtons.length; i++) {
                    $(".role_menu[data-button='" + hideButtons[i] + "']").hide();
                }
                for (var s = 0; s < menuList.length; s++) {
                    $(".role_menu[data-button='" + menuList[s] + "']").show();
                }
            }
            if(data.code == 999){
                for (var s = 0; s < buttons.length; s++) {
                    $(".role_menu[data-button='" + buttons[s] + "']").hide();
                }
            }
        })
    }
}

function getArrDifference(arr1, arr2) {
    return arr1.concat(arr2).filter(function (v, i, arr) {
        return arr.indexOf(v) === arr.lastIndexOf(v);
    });
}
